package models;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import play.data.validation.Constraints.Min;
import play.db.ebean.Model;

@Entity
public class OrderItem extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9119511737209227712L;

	@Id
	private Long id;

	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL,
			CascadeType.PERSIST, CascadeType.REMOVE })
	@JoinColumn(name = "product_id")
	private Product product;

	@Min(1)
	private int amount;

	public static void create(OrderItem o) {
		o.save();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((getProduct() == null) ? 0 : getProduct().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		OrderItem other = (OrderItem) obj;
		if (getProduct() == null) {
			if (other.getProduct() != null) {
				return false;
			}
		} else if (!getProduct().equals(other.getProduct())) {
			return false;
		}
		return true;
	}

}
